﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsuserdepositrulesServices : Repository<corecmsuserdepositrules>, IcorecmsuserdepositrulesServices
    {
        public bool InsertAsync(corecmsuserdepositrules entity)
        {
            return base.Change<corecmsuserdepositrules>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsuserdepositrules> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsuserdepositrules entity)
        {
            return base.Change<corecmsuserdepositrules>().Update(a => new corecmsuserdepositrules()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                depositType = entity.depositType,
                depositMoney = entity.depositMoney,
                giveMoney = entity.giveMoney,
                giveIntegral = entity.giveIntegral,
                upGiveIntegral = entity.upGiveIntegral,
                twoUpGiveIntegral = entity.twoUpGiveIntegral,
                depositRuleStatus = entity.depositRuleStatus,
                isAccumulation = entity.isAccumulation,
                startTime = entity.startTime,
                endTime = entity.endTime,
                isDelete = entity.isDelete,
                createTime = entity.createTime,
                remark = entity.remark,
                couponId = entity.couponId,
                couponCount = entity.couponCount,
                userCategoryId = entity.userCategoryId,
                modifyTime = entity.modifyTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsuserdepositrules> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsuserdepositrules>().Update(a => new corecmsuserdepositrules()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    depositType = item.depositType,
                    depositMoney = item.depositMoney,
                    giveMoney = item.giveMoney,
                    giveIntegral = item.giveIntegral,
                    upGiveIntegral = item.upGiveIntegral,
                    twoUpGiveIntegral = item.twoUpGiveIntegral,
                    depositRuleStatus = item.depositRuleStatus,
                    isAccumulation = item.isAccumulation,
                    startTime = item.startTime,
                    endTime = item.endTime,
                    isDelete = item.isDelete,
                    createTime = item.createTime,
                    remark = item.remark,
                    couponId = item.couponId,
                    couponCount = item.couponCount,
                    userCategoryId = item.userCategoryId,
                    modifyTime = item.modifyTime
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsuserdepositrules>().Update(a => new corecmsuserdepositrules()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsuserdepositrules>().Update(a => new corecmsuserdepositrules()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsuserdepositrules> GetCaChe()
        {
            return base.GetList();
        }

        #endregion
        public List<corecmsuserdepositrules> SelectByMoney(int corpId, int storeId, decimal Money)
        {
            return base.GetList(a => a.corpId == corpId && a.storeId == storeId && a.isDelete == false && a.depositRuleStatus == 1 && a.depositMoney == Money).OrderBy(a => a.id).ToList();
        }

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuserdepositrules> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsuserdepositrules> custos = new List<corecmsuserdepositrules>();
            var where = Expressionable.Create<corecmsuserdepositrules>();
            where = where.And(a => a.isDelete == false && a.depositRuleStatus == 1);
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.name.Contains(sName))
            //        .Or(a => a.number.Contains(sName)).Or(a => a.name.Contains(sName));
            //}
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuserdepositrules> QueryAsync(long typeId)
        {
            List<corecmsuserdepositrules> list = new List<corecmsuserdepositrules>();
            var where = Expressionable.Create<corecmsuserdepositrules>();
            where = where.And(a => a.isDelete == false && a.depositRuleStatus == 1 && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.id == typeId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();

            return list;
        }

        #endregion

    }
}
